﻿<!DOCTYPE html>
<html>
<head>
    <title>圣经</title>
    <meta charset="UTF-8">
    <link rel="icon" href="/assets/zw-ico.jpg">
    <div th:include="header::headPart"></div>
	<script type="text/javascript" th:inline="javascript">
		$(document).ready(function() {
			$("#major").css("table-layout", "fixed");
			initData();
			initLang();
		});
			
		var bid = [[${bid}]];
		var currentcid = 0;
		var version = [[${version}]];
		
		function initData() {
			var bname = [[${bname}]];
			var cid = [[${cid}]];
			currentcid = cid;
			var cids = [[${cids}]];
			var lines = [[${lines}]];
			showBookNameAndChapters(bid, bname, cid, cids);
			showVerses([[${lines}]]);
		}
		
		function initLang() {
			$('input[type=radio][name=lang]').change(function() {
				version = this.value;
				switchLang();
			});
			
			$("#" + version).attr("checked", "checked");
		}
		
		function switchLang() {
			console.log(bid, currentcid);
			
			var weburl = "/sirap/bibleRest/verses/" + bid + "/" + currentcid + "?v=" + version;
			var theball = {
				url : weburl,
				dataType : "json",
				success: function(meat) {
					showBookNameAndChapters(meat.bid, meat.bname, meat.cid, meat.cids);
					showVerses(meat.lines);
		        }
			}
			$.ajax(theball);
		}
		
		function fetchVerses() {
			console.log($(this));
			currentcid = $(this).data("cid");
			console.log(bid, currentcid);

			var weburl = "/sirap/bibleRest/verses/" + bid + "/" + currentcid + "?v=" + version;
			var theball = {
				url : weburl,
				dataType : "json",
				success: function(meat) {
					showBookNameAndChapters(meat.bid, meat.bname, meat.cid, meat.cids);
					showVerses(meat.lines);
		        }
			}
			$.ajax(theball);
		}		
		
		function showVerses(lines) {
			if(!lines) {
				return;
			}
			$("#mybody").empty()
			var vcount = 0;
			var fullversion = lines[0];
			var originhref = lines[1];
			var aorigin = $("<a></a>").attr("href", originhref).text("$" + fullversion).attr("target", "_blank");
			var start = 2;
			for(var i = start; i < lines.length; i++) {
				var line = lines[i];
				console.log(i, line);
				var row = $("<tr></tr>");
				var td = $("<td></td>").css("word-wrap", "break-word");
				if(i == start) {
					var kid = $("<i></i>").append(aorigin);
					td.append(kid);
				} else if(line.startsWith("&")) {
					var kid = $("<font></font>").text(line).attr("weight", "bold").attr("size", "5");
					td.append(kid);
				} else {
					if(vcount % 2 == 0) {
			    		row.css("background", "rgb(189,219,245)");
					}
					vcount++;
					var kid = $("<font></font>").text(line).attr("size", "4");
					td.append(kid);
				}
				row.append(td);
				$("#mybody").append(row);
			}
			
			{	//donation
				var boxMoney = $("<input></input>").attr("type", "checkbox");
				boxMoney.css("vertical-align", "middle");
				boxMoney.addClass("cold");
				boxMoney.click(function() {
					var div = $(this).parent().find(".warm");
					if ($(this).prop("checked")) {
						div.show();
					} else {
						div.hide();
					}
				});

				var infoMoney = $("<input></input>").attr("type", "button").val("光明正大打赏，心安理得笑纳").addClass("btn btn-warning");
				infoMoney.click(function() {
					$(this).parent().find(".cold").click();
				});

				var image = $("<img></img>").attr("alt", "<打赏二维码，点击加载>");
				image.attr("src", "/storage/donation.weixin.foxi.png");
				image.css("max-width", "100%");
				
				var divBrImage = $("<div></div>");
				divBrImage.addClass("warm");
				divBrImage.append(image);
				divBrImage.hide();
				
				var td = $("<td></td>");
				
				td.append(boxMoney).append("&nbsp;").append(infoMoney);
				td.append(divBrImage);
				var tr = $("<tr></tr>").append(td);
				$("#mybody").append(tr);
			}
		}
		
		function showBookNameAndChapters(bid, bname, cidParam, cids) {
			var styles = ["primary", "success", "info", "warning", "danger"];
			var tdBookname = $("#tdBookname").empty();
			var bookInfo = "#" + bid + ",    " + bname;
			var title = "圣经" + bname + " " + cidParam;
			htmlTitle(title);
			var styleA = styles[0];
			var labelA = $("<label></label>").text(bookInfo).addClass("label label-" + styleA).css("font-size", 16);
			tdBookname.css("word-wrap", "break-word");
			tdBookname.append(labelA);
			var tdChapters = $("#tdChapters").empty();
			tdChapters.css("word-wrap", "break-word");
		//	console.log(cids);
			for(var kdx = 0; kdx < cids.length; kdx++) {
				///bible/verses/12/43
				var cid = cids[kdx];
				//console.log(kdx, cid, cidParam);
				var styleB = cid == cidParam ? "success" : "default";
				var labelB = $("<input></input>").attr("type", "button").val(cid).addClass("btn btn-sm btn-" + styleB);
				var chapter = $("<a></a>").append(labelB).click(fetchVerses);
				chapter.attr("data-cid", cid);
				tdChapters.append(chapter);
				if(kdx != cids.length) {
					tdChapters.append("&nbsp;&nbsp;");
				}
			}
		}
		
		function backto() {
			var url = "/sirap/bible/books?v=" + version;
			window.location.href = url;
		}
	</script>
	<style type="text/css">
	.results tr[visible='false'],
	     .no-result{
	       display:none;
	     }
	     .results tr[visible='true']{
	       display:table-row;
	     }
     .counter{
       padding:8px; 
       color:#ccc;
     }
      
	.h100 {
		height:400px;
	}
	.m40 {
		margin: 10px;
	}
	.middlecenter {
		vertical-align:middle; 
		text-align:center;
	}
	</style>
</head>

<body>
	<div th:replace="header::bodyHead"></div>
	<div class="margin10">
		<div id="alert"></div>
		<div>
		<h2 id="top">查看章节&nbsp;<input id="backto" class="btn btn-primary btn-sm" type="button" onclick="backto();" value="圣经目录" /></h2>
		<div class="radio">
			<label>
				<input type="radio" name="lang" id="CNVS" value="CNVS" checked>简体中文
			</label>&nbsp;&nbsp;
			<label>
				<input type="radio" name="lang" id="MEV" value="MEV">English
			</label>&nbsp;&nbsp;
			<label>
				<input type="radio" name="lang" id="JBS" value="JBS">Español
			</label>
		</div>
		</div>
		
	    <table id="major" class="table table-hover">
	    	<thead>
	    		<tr>
	    			<td>
	    				<span id="tdBookname"></span>
	    				&nbsp;&nbsp;
		    			<select id="category" class="input-sm">
		    				<option>sam</option>
		    			</select>
	    			</td>
	    		</tr>
	    		<tr>
	    			<td id="tdChapters"></td>
	    		</tr>
	    	</thead>
	        <tbody id="mybody">
	        </tbody>
	        <tr>
			  <td>
			  	<a href="#top">回到顶部</a>
			  </td>
			</tr>
	    </table>
	    </div>
	<div th:replace="footer::copyright"></div>
</body>
</html>